/*
 * Copyright (C) 2011 Dirk Weißenborn, dirk.weissenborn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.weissenborn.colabtex.client.activities;


import com.weissenborn.colabtex.client.ClientFactory;
import com.weissenborn.colabtex.client.events.LogOutEvent;
import com.weissenborn.colabtex.client.places.Dashboard;
import com.weissenborn.colabtex.client.places.Login;
import com.weissenborn.colabtex.client.service.EtherpadService;
import com.weissenborn.colabtex.client.service.EtherpadServiceAsync;
import com.weissenborn.colabtex.client.view.HeaderView;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

/**
 * The Class HeaderActivity which controls the view of the header
 */
public class HeaderActivity extends AbstractActivity implements HeaderView.Presenter {
	
	/** The client factory. */
	private ClientFactory clientFactory;

	/**
	 * Instantiates a new header activity.
	 *
	 * @param clientFactory the client factory
	 */
	public HeaderActivity(ClientFactory clientFactory) {
		this.clientFactory = clientFactory;
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget, com.google.gwt.event.shared.EventBus)
	 */
	public void start(AcceptsOneWidget containerWidget, final EventBus eventBus) {
		HeaderView view = clientFactory.getHeaderView();		
		view.setPresenter(this);
		
		containerWidget.setWidget(view.asWidget());
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.HeaderView.Presenter#goTo(com.google.gwt.place.shared.Place)
	 */
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.HeaderView.Presenter#logout()
	 */
	public void logout() {
		clientFactory.getEventBus().fireEvent(new LogOutEvent());
		goTo(new Login(new Dashboard()));
	}

	/* (non-Javadoc)
	 * @see com.weissenborn.colabtex.client.view.HeaderView.Presenter#leaveCurrentGroup()
	 */
	public void leaveCurrentGroup() {
		String groupName = clientFactory.getGroupListView().getCurrentGroup();
		if(groupName == null)
			Window.alert("No group selected");
		
		EtherpadServiceAsync service = EtherpadService.Util.getInstance();
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {	
			public void onSuccess(Boolean result) {
				if(result) {
					Window.alert("Successfully left group!");
					clientFactory.getUser().setPermissions();
				}
				else
					Window.alert("Couldn't leave group because you are the only left admin!");
			}
			
			public void onFailure(Throwable caught) {
				Window.alert("No connection to server");
			}
		};
		service.removePermission(groupName,clientFactory.getUser().getId(),true, callback);
	}
}
